package com.gh.manager.clazz.service.impl;

import java.util.List;

import com.gh.common.core.domain.entity.SysUser;
import com.gh.common.utils.DateUtils;
import com.gh.manager.clazz.domain.GhUserClazz;
import com.gh.manager.clazz.mapper.GhUserClazzMapper;
import com.gh.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gh.manager.clazz.mapper.GhClazzMapper;
import com.gh.manager.clazz.domain.GhClazz;
import com.gh.manager.clazz.service.IGhClazzService;

/**
 * 班级Service业务层处理
 * 
 * @author andas
 * @date 2024-12-26
 */
@Service
public class GhClazzServiceImpl implements IGhClazzService 
{
    @Autowired
    private GhClazzMapper ghClazzMapper;

    @Autowired
    private GhUserClazzMapper ghUserClazzMapper;

    /**
     * 查询班级
     * 
     * @param clazzId 班级主键
     * @return 班级
     */
    @Override
    public GhClazz selectGhClazzByClazzId(Long clazzId)
    {
        return ghClazzMapper.selectGhClazzByClazzId(clazzId);
    }

    /**
     * 查询班级列表
     * 
     * @param ghClazz 班级
     * @return 班级
     */
    @Override
    public List<GhClazz> selectGhClazzList(GhClazz ghClazz)
    {
        return ghClazzMapper.selectGhClazzList(ghClazz);
    }

    /**
     * 新增班级
     * 
     * @param ghClazz 班级
     * @return 结果
     */
    @Override
    public int insertGhClazz(GhClazz ghClazz)
    {
        ghClazz.setCreateTime(DateUtils.getNowDate());
        return ghClazzMapper.insertGhClazz(ghClazz);
    }

    /**
     * 修改班级
     * 
     * @param ghClazz 班级
     * @return 结果
     */
    @Override
    public int updateGhClazz(GhClazz ghClazz)
    {
        ghClazz.setUpdateTime(DateUtils.getNowDate());
        return ghClazzMapper.updateGhClazz(ghClazz);
    }

    /**
     * 批量删除班级
     * 
     * @param clazzIds 需要删除的班级主键
     * @return 结果
     */
    @Override
    public int deleteGhClazzByClazzIds(Long[] clazzIds)
    {
        return ghClazzMapper.deleteGhClazzByClazzIds(clazzIds);
    }

    /**
     * 删除班级信息
     * 
     * @param clazzId 班级主键
     * @return 结果
     */
    @Override
    public int deleteGhClazzByClazzId(Long clazzId)
    {
        return ghClazzMapper.deleteGhClazzByClazzId(clazzId);
    }

    @Override
    public String assignPlayer(List<Long> userIds, Long clazzId) {
        try {
            if (userIds != null && !userIds.isEmpty()) {
                for (Long userId : userIds) {
                    // 确保 userId 是 Long 类型
                    if (userId == null) {
                        throw new IllegalArgumentException("用户ID不能为空");
                    }
                    GhUserClazz ghUserClazz = new GhUserClazz();
                    ghUserClazz.setClazzId(clazzId);
                    ghUserClazz.setUserId(userId);
                    int rowsAffected = ghUserClazzMapper.insertGhUserClazz(ghUserClazz);
                    if (rowsAffected <= 0) {
                        throw new RuntimeException("插入用户班级关系失败");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "操作失败: " + e.getMessage();
        }
        return "操作成功";
    }


    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public List<SysUser> selectClazzDetails(GhClazz ghClazz) {
        GhUserClazz ghUserClazz = new GhUserClazz();
        ghUserClazz.setClazzId(ghClazz.getClazzId());
        List<GhUserClazz> ghUserClazzes = ghUserClazzMapper.selectGhUserClazzList(ghUserClazz);
        if(ghUserClazzes!=null && ghUserClazzes.size()!=0){
            List<SysUser> users = new java.util.ArrayList<>();
            for(int i = 0;i<ghUserClazzes.size();i++){
                SysUser sysUser = new SysUser();
                sysUser.setUserId(ghUserClazzes.get(i).getUserId());
                users.add(sysUser);
            }
            List<SysUser> sysUsers = sysUserMapper.selectListUser(users);
            return sysUsers;
        }
        return null;
    }
}
